package com.zjbti.notepad;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.zjbti.notepad.utils.DBUtils;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_time,tv_main_title,tv_back;
    private EditText et_title,et_content;
    private ImageView iv_delt,iv_save;
    private DBUtils dbUtils;
    private int id;



    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        initView();
        initData();

    }

    private void initData(){
        dbUtils=new DBUtils(this);
        Intent intent=getIntent();
        id=intent.getIntExtra("id",0);
        if(id!=0){
            tv_main_title.setText("修改记录");
            et_title.setText(intent.getStringExtra("title"));
            et_content.setText(intent.getStringExtra("content"));
            tv_time.setText(intent.getStringExtra("time"));
            tv_time.setVisibility(View.VISIBLE);
        }else{
            tv_main_title.setText("添加记录");
            tv_time.setVisibility(View.GONE);
        }
    }

    private void initView() {
        tv_back=findViewById(R.id.tv_return);
        tv_back.setVisibility(View.VISIBLE);
        et_title=findViewById(R.id.et_title);
        et_content=findViewById(R.id.et_content);
        tv_time=findViewById(R.id.tv_time);
        iv_delt=findViewById(R.id.iv_delt);
        iv_save = findViewById(R.id.iv_savs);
        tv_main_title=findViewById(R.id.tv_main_title);
        tv_back.setOnClickListener(this);
        iv_delt.setOnClickListener(this);
        iv_save.setOnClickListener(this);
     }

    /**
     * @param v
     */
    public void onClick(View v) {

        switch (v.getId()) {
             case R.id.tv_return:
                 finish();
                 break;
             case R.id.iv_delt:
                 et_content.setText("");
                 break;
             case R.id.iv_savs:
                 String title=et_title.getText().toString().trim();
                 String content= et_content.getText().toString().trim();
                 if (id!=0){
                     if (content.length()>0){
                        if (dbUtils.updateNote(id,title,content,DBUtils.getTime())){
                              showToast("修改成功");
                              setResult(2);
                              finish();
                        }else {
                               showToast("修改失败");
                        }
                     }else {
                         showToast("修改内容不能为空！");
                     }
                 }else {
                     if(content.length()>0) {
                         if (dbUtils.saveNote(title, content, DBUtils.getTime())) {
                             showToast("保存成功");
                             setResult(2);
                             finish();
                         } else {
                             showToast("保存失败");
                         }
                     }else{
                         showToast("添加的内容不能为空");
                     }
                 }
                 break;
         }
    }

    private void showToast(String message) {
        Toast.makeText(RecordActivity.this, message, Toast.LENGTH_SHORT).show();
    }
}
